package com.hsy.baoinbao.modules.order.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseFragment;
import com.hsy.baoinbao.modules.order.activity.DetailGoodsActivity;
import com.hsy.baoinbao.modules.order.adapter.GoodsAdapter;
import com.hsy.baoinbao.modules.order.adapter.GoodsAllAdapter;
import com.hsy.baoinbao.modules.order.adapter.GoodsDoneAdapter;
import com.hsy.baoinbao.modules.order.adapter.ReceivingAdapter;
import com.hsy.baoinbao.modules.order.interfaces.IFragmentUpdateListener;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.pullableview.PullableListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hsy on 2016/3/23.
 */
public class FragmentGoodsAll extends CommonBaseFragment implements IFragmentUpdateListener {
    private PullableListView goods_all_list;
    private GoodsAllAdapter goodsAdapter;
    private List<HashMap<String, String>> listData;
    private PullToRefreshLayout list_layout;
    private int firstRow = 0, total = 0, current = 10;
    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {

            if (0 == what) {
                listData.clear();
                showView(response);
                firstRow = firstRow + current;
                list_layout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            if (1 == what) {
                showView(response);
                firstRow = firstRow + current;
                current = current + 10;
                list_layout.loadmoreFinish(PullToRefreshLayout.LOAD_FINISH);
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
            list_layout.refreshFinish(PullToRefreshLayout.FAIL);
        }
    };

    private void showView(String response) {

        try {
            JSONObject result = new JSONObject(response);
            total = result.optInt("count");
            JSONArray lists = result.optJSONArray("lists");

            for (int i = 0; i < lists.length(); i++) {
                String address = lists.optJSONObject(i).optString("address");
                String order_time = lists.optJSONObject(i).optString("order_time");
                String price = lists.optJSONObject(i).optString("price");
                String code = lists.optJSONObject(i).optString("code");
                String status = lists.optJSONObject(i).optString("status");
                String id = lists.optJSONObject(i).optString("id");
                String contact_phone = lists.optJSONObject(i).optString("contact_phone");
                String name_merchant = lists.optJSONObject(i).optString("name_merchant");
                HashMap<String, String> map = new HashMap<>();
                map.put("id", id);
                map.put("item_goods_title", "全部");
                map.put("price", price);
                map.put("time", order_time);
                map.put("address", address);
                map.put("code", code);
                map.put("status", status);
                map.put("name_merchant", name_merchant);
                map.put("contact_phone", contact_phone);
                listData.add(map);
            }
            goodsAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        CommonUtils.hideProgressDialog();


    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_goods_all;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        goods_all_list = F(R.id.goods_all_list);
        list_layout = F(R.id.list_layout);
        listData = new ArrayList<>();
        goodsAdapter = new GoodsAllAdapter(getAttchedActivity(), listData);
        goods_all_list.setAdapter(goodsAdapter);
        list_layout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                if (listData == null) {
                    list_layout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    return;
                }
                listData.clear();
                firstRow = 0;
                current = 10;
                onFragmentUpdate();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if (total == 0 || current >= total) {
                    showToast("没有更多了");
                    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.LOAD_FINISH);
                    return;
                }
                Map<String, String> data = new HashMap<>();
                data.put("token", CommonUtils.getToken(getAttchedActivity()));
                data.put(Consts.FIRST_ROW, String.valueOf(firstRow));
                data.put(Consts.LIST_ROWS, String.valueOf(current));
                HttpUtil.httpPostWithParams(getAttchedActivity(), 1, Consts.API_GET_MALL_LISTS, data, httpCallBack);
            }
        });
        goods_all_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getAttchedActivity(), DetailGoodsActivity.class);
                intent.putExtra("id", listData.get(position).get("id"));
                DPLogUtil.d("id==" + listData.get(position).get("id"));
                getParentFragment().getActivity().startActivityForResult(intent, 2);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        onFragmentUpdate();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onFragmentUpdate() {
        CommonUtils.showProgressDialog(getAttchedActivity());
        Map<String, String> data = new HashMap<>();
        data.put("token", CommonUtils.getToken(getAttchedActivity()));
        data.put(Consts.FIRST_ROW, String.valueOf(firstRow));
        data.put(Consts.LIST_ROWS, String.valueOf(current));
        HttpUtil.httpPostWithParams(getAttchedActivity(), 0, Consts.API_GET_MALL_LISTS, data, httpCallBack);
    }
}
